package com.zjy.product.service.impl;

import com.zjy.basic.service.BaiduAiAuditService;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.CodeGenerateUtils;
import com.zjy.basic.util.LoginContext;
import com.zjy.order.domain.OrderProduct;
import com.zjy.order.mapper.OrderProductMapper;
import com.zjy.org.domain.Employee;
import com.zjy.org.mapper.EmployeeMapper;
import com.zjy.product.domain.Product;
import com.zjy.product.domain.ProductDetail;
import com.zjy.product.domain.Product;
import com.zjy.product.domain.ProductOnlineAuditLog;
import com.zjy.product.domain.Product;
import com.zjy.product.mapper.ProductDetailMapper;
import com.zjy.product.mapper.ProductMapper;
import com.zjy.product.mapper.ProductOnlineAuditLogMapper;
import com.zjy.product.service.ProductService;
import com.zjy.basic.service.impl.BaseServiceImpl;
import com.zjy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;

    @Transactional
    @Override
    public void add(Product product) {
        super.add(product);
        //获取宠物详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            //添加宠物详情
            productDetailMapper.add(productDetail);
        }
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetailMapper.updateDetailByProductId(product);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        productDetailMapper.delProductDetailByProductId(id);
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public AjaxResult onsale(Long[] ids, HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //遍历 选中要上架的product的id
        for (Long id:ids){
            //1.先取得对应id的对象
            Product product = productMapper.findById(id);
            //2.审核：百度智能审核
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());//先审核名称
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());//再审核图片
            System.out.println(boo1+"   "+boo2);
            if (boo1 && boo2){//如果审核成功（都为true）
                //2.审核通过：修改状态state=1，上架时间 - 添加审核日志
                productMapper.updateOnSaleState(id,new Date());

                //生成审核日志对象
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                //添加审核日志信息
                log.setProductId(id);
                log.setState(1);
                log.setNote("审核成功");
                log.setAuditId(employee.getId());
                //添加到数据库
                productOnlineAuditLogMapper.add(log);
            }else {//审核不通过
                //3.审核失败：添加审核日志
                //生成审核日志对象
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                //添加审核日志信息
                log.setProductId(id);
                log.setState(0);
                log.setNote("审核失败");
                log.setAuditId(employee.getId());
                //添加到数据库
                productOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(Long[] ids) {
        //修改状态
        productMapper.updateState(ids,new Date());
    }

    @Override
    public void accept(Product product) {
        OrderProduct order = new OrderProduct();
//        生成订单
//        订单号
        String sn = CodeGenerateUtils.generateOrderSn(product.getId());
        order.setOrderSn(sn);
//        摘要
        order.setDigest("[服务订单]购买1次"+product.getName());
//        价格
        order.setPrice(product.getSaleprice());
//       状态
        order.setState(0);

        orderProductMapper.add(order);
    }
}
